//
//  WeeklyLetterView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/2/21.
//

import SwiftUI

//来电历史
struct CallHistoryiew: View {
    @StateObject var viewModel = CallHistoryModel()
    var body: some View {
        VStack(alignment: .leading,spacing : 0) {
            Text("来电记录")
                .font(.system(size: 26))
                .foregroundStyle(.bgNormalText2)
                .fontWeight(.medium)
            Text("所有的来电都会记录在这边")
                .font(.system(size: 14))
                .foregroundStyle(.bgNormalText3)
                .padding(.top, 5)
                .padding(.bottom, 20)
            ZStack {
                if !viewModel.status.isEmpty {
                    HomeEmptyView(text1: viewModel.status)
                        .frame(maxWidth: .infinity,maxHeight: .infinity)
                } else {
                    ScrollView {
                        //需要嵌套一层LazyVStack，不然会产生间距，而且大小还会随机
                        LazyVStack(spacing: 20) {
                            ForEach(Array(viewModel.items.enumerated()), id: \.element.objectId) { index, item in
                                CallHistoryItemView(item: item)
                                    .contextMenu {
                                        Button("删除", role: .destructive) {
                                            viewModel.doDel(index: index, bean: item) {
                                                withAnimation {
                                                    if !viewModel.items.isEmpty {
                                                        viewModel.items.remove(at: index)
                                                    }
                                                }
                                            }
                                        }
                                    }
                            }
                            LoadMoreFooter(refreshing: $viewModel.footerRefreshing, action: {
                                viewModel.getData(false)
                            }) {
                                LoadMoreView(noMore: viewModel.noMore, footerRefreshing: viewModel.footerRefreshing)
                            }
                            .noMore(viewModel.noMore)
                            .preload(offset: 10)
                        }
                    }
                    .enableRefresh()
                }
            }.frame(maxWidth: .infinity, maxHeight: .infinity)
        }.frame(maxWidth: .infinity,maxHeight: .infinity,alignment: .topLeading)
            .padding(.horizontal,20)
            .padding(.top,10)
            .navigationTitle("")
            .background(.bg)
            .onAppear{
                if !viewModel.isInit {
                    viewModel.getData(true)
                    viewModel.isInit = true
                }
            }
    }
}

#Preview {
    CallHistoryiew()
}
